<template>
	<view>
	    <view class="saoyisao">
	        <view class="ssbtn" @click="saoyisao">扫一扫</view>
	    </view>
	    <view class="pagetitle" >
	      <view>收货网点信息</view>
	      <view class="hejiguan"></view>
	    </view>
	    <view class="trth" >
	       <view class="proname">网点编号</view>
	       <view class="spec" >{{netcode}}</view>
	    </view>
	    <view class="trth" >
	       <view class="proname">网点名称</view>
	       <view class="spec" >{{wdname}}</view>
	    </view>
	    <view class="trth" v-if="wdaddr">
	       <view class="proname">网点地址</view>
	       <view class="spec" >{{wdaddr}}</view>
	    </view>
	
	    <view class="pagetitle" v-if="productlist.length > 0">
	      <view>产品信息</view>
	      <view class="hejiguan"></view>
	    </view>
	    <view class="trth" v-if="productlist.length > 0">
	       <view class="proname">产品名称</view>
	       <view class="spec">产品规格</view>
	    </view>
	    <view class="trth" v-for="item in productlist">
	       <view class="proname2">{{item.ProductName}}</view>
	       <view class="spec2">{{item.Norm}}</view>
	    </view>
	    <view class="pagetitle" v-if="boxcodelist.length > 0">
	      <view>待发货箱码</view>
	      <view class="hejiguan">总计: {{boxcodelist.length}} 箱</view>
	    </view>
	    <view class="trth" v-for="(item,index) in boxcodelist" >
	       <view class="proname3">{{item}}</view>
	       <view class="spec3 red" @click="delete_boxcode(index)" >删除</view>
	    </view>
	    <view class="pagetitle" v-if="sncodelist.length > 0">
	      <view>待发货罐码</view>
	      <view class="hejiguan">总计: {{sncodelist.length}} 罐</view>
	    </view>
	    <view class="trth" v-for="(item,index) in sncodelist" >
	       <view class="proname3">{{item}}</view>
	       <view class="spec3 red" @click="delete_sncode(index)">删除</view>
	    </view>
		<view v-if="isclick == 0">
			<view class="savebtn" @click="savecode()"
			v-if="boxcodelist.length > 0 || sncodelist.length > 0 ">提交发货</view>
			
		</view>
	    <view v-if="isclick == 1"> <view class="savebtn2">提交中</view></view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
			  netcode:"",
			  wdname:"",
			  wdaddr:"",
			  isclick:0,
			  maxsn:100,
			  maxbox:50,
			  sncodelist:[],//罐码列表
			  boxcodelist:[], //箱码列表
			  productlist:[] //产品信息列表
			}
		},
		 onLoad(p) {
		  if(p.wd){
			this.netcode=p.wd
			this.wdname=p.wdname
			//this.wdaddr=p.wdaddr
		  }else{
			uni.showToast({  title: '缺失参数',icon:"none"})
			setTimeout(function(){
			  uni.navigateBack({delta:1})
			},2000)
		  }
		  uni.showModal({
		  	content:"一次出货量最大为"+this.maxsn+"听或"+this.maxbox+"箱",
		  	showCancel:false,
		  	title:"温馨提示"
		  })
		},
		onShow(){},
		methods: {
		   //验证罐码是否被使用
			get_check_boxcode(boxcode){
				var data={}
				var that=this;
				data.boxcode=boxcode;
				data.jxs_code=uni.getStorageSync('fahuo_username').user_code;
				data.wdbh=that.netcode;
				data.user_id=uni.getStorageSync("user_id")
				uni.showLoading({ title: '请求中...', });
				
				this.request('/api/duoqudao/check_boxcode_vail', data,"post").then(ret => {
					uni.hideLoading()
					if(ret.code == 1){
						uni.showModal({ title: '温馨提示',content:ret.msg, showCancel:false});
						return
					}else{
						var type=ret.type
						if(type == 1){
						  let sncodelist=that.sncodelist
						  let checkret=false;
						  for(var i=0;i<sncodelist.length;i++){
							 if(sncodelist[i] == ret.data){ checkret=true;break;}
						  }
						  if(checkret){
							uni.showToast({  title: '该罐码已扫,请勿重复扫码',icon:"none"})
							return
						  }else{
							if(that.sncodelist.concat(ret.data).length > that.maxsn){
								uni.showToast({  title: '到达最大出货量，该次不计入',icon:"none"})
								return
							}
							that.sncodelist=that.sncodelist.concat(ret.data)
							that.set_productlist(ret.data,1)
						  }
						}else if(type == 2){
						  let boxcodelist=that.boxcodelist
						  let checkret=false;
						  for(var i=0;i<boxcodelist.length;i++){
							 if(boxcodelist[i] == ret.data){ checkret=true;break;}
						  }
						  if(checkret){
							  uni.showToast({  title: '该箱码已扫,请勿重复扫码',icon:"none"})
							  return
						  }else{
							if(that.boxcodelist.concat(ret.data).length > that.maxbox){
								uni.showToast({  title: '到达最大出货量，该次不计入',icon:"none"})
								return
							}
							that.boxcodelist=that.boxcodelist.concat(ret.data);
							that.set_productlist(ret.data,2)
						  }
						}
					}
				})
			},
			//删除罐码
			delete_sncode(i){
			   let sncodelist=this.sncodelist;
			   let productlist=this.productlist
			   for(var j=0;j<productlist.length;j++){
				  if(productlist[j].TrackCode == sncodelist[i]){
					productlist.splice(j,1);
					break;
				  }
			   }
			   sncodelist.splice(i,1);
			   this.sncodelist=sncodelist;
			   this.productlist=productlist;
			},
			//删除箱码
			delete_boxcode(i){
			  let boxcodelist=this.boxcodelist;
			  let productlist=this.productlist
			  for(var j=0;j<productlist.length;j++){
				 if(productlist[j].BoxCode == boxcodelist[i]){ productlist.splice(j,1);break; }
			  }
			  boxcodelist.splice(i,1);
			  this.boxcodelist=boxcodelist
			  this.productlist=productlist
			},
			set_productlist(boxcode,type){
			  let data={}
			  data.boxcode=boxcode
			  data.type=type
			  let that=this
			  this.request('/api/duoqudao/select_product_info', data,"post").then(ret => {
					let productlist=that.productlist
					let product=ret.data
					let handle=false;
					for(var i=0;i<productlist.length;i++){
						if(productlist[i].ProductCD == product.ProductCD){ handle=true;break;}
					}
					if(!handle){
					  that.productlist=that.productlist.concat(product);
					}
			  })
			},
			saoyisao(){
			  var that=this
			  uni.scanCode({
					success: (res) => {
						if(res.errMsg=='scanCode:ok'){
						  var result = res.result;
						  that.get_check_boxcode(result);
						}else{
							uni.showToast({ title: '识别失败！',icon:"none" })
						}
					}
			  })
			},
			//提交发货
			savecode(){
			  let sncodelist=this.sncodelist
			  let boxcodelist=this.boxcodelist
			  var that = this;
			  if(sncodelist.length == 0 && boxcodelist.length == 0){
				  uni.showToast({ title: '请扫一扫',icon:"none"})
				  return
			  }
			  if(this.isclick){
				  uni.showToast({ title: '请勿重复提交',icon:"none"})
				  return
			  }
			  this.isclick=1
			  uni.showModal({
			  	title:"温馨提示",
				content:"确认提交吗？",
				success:function(retss){
					if(retss.confirm){
						var data={}
						uni.showLoading({ title:"提交中..请稍后"})
						data.sncodelist=sncodelist.join(",")
						data.boxcodelist=boxcodelist.join(",")
						data.wdbh=that.netcode
						data.jxs_code=uni.getStorageSync('fahuo_username').user_code;
						data.jxs_name=uni.getStorageSync('fahuo_username').username;
						data.wdbh_name=that.wdname;
						data.role=uni.getStorageSync('fahuo_username').role;
						that.request('/api/duoqudao/product_fahuo_wd', data,"post").then(ret => {
							//this.isclick=0
							uni.hideLoading()
							uni.showToast({ title: '提交发货成功',icon:"none"})
							setTimeout(function(){
							  uni.navigateBack({delta:2})
							},3000)
						})
					}else{
						that.isclick=0
					}
				}
			  })
			}
		}
	}
</script>

<style>
.trth{
  display: flex;
  height: 35px;
  line-height: 35px;
  width: calc(100% - 40px);
  margin: 0 auto;
  font-size: 14px;
  border-bottom: 1px solid #eee;
  background-color: #fff;
  padding-left: 10px;
}
.savebtn2{
  font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
  text-align: center;
  width: 60%;
  margin: 0 auto;
  height: 40px;
  line-height:40px;
  background-color: #9E9E9E;
  color: #fff;
  border-radius: 5px;
  position: fixed;
  bottom: 30px;
  left: 20%;
}
.proname{
   width: 80px;
   color: #999;
}
.spec{
  width:calc(100% - 80px);
  color: #999;
  overflow: hidden;
  height: 35px;
  white-space: nowrap;
  text-overflow: ellipsis;
}
.proname2{font-size: 14px;width: 80%;overflow: hidden;white-space: nowrap;text-overflow: ellipsis;}
.spec2{font-size: 14px;width: 30%;}
.pagetitle{font-size: 16px;margin-left: 20px;height: 45px;line-height: 45px;display: flex;
  justify-content:space-between;}
.hejiguan{font-size: 14px;padding-right: 20px;color: #333;}
.saoyisao{height: 200px;line-height: 200px;text-align: center;}
.red{color: #969696;}
.spec3{font-size: 14px;width: 20%;}
.proname3{
  width: 80%;
  color: #999;
}
page{background-color: #eee;padding-bottom: 100px;}
.ssbtn{
  width: 100px;
  height: 100px;
  margin: 0 auto;
  text-align: center;
  background-color: #1B764A;
  color: #fff;
  letter-spacing: 2px;
  font-weight: bold;
  font-family: "思源黑体";
  line-height: 100px;
  position: relative;
  top: 80px;
  border-radius: 50%;
}
.savebtn{
  font-family: 'Franklin Gothic Medium', 'Arial Narrow', Arial, sans-serif;
  text-align: center;
  width: 60%;
  margin: 0 auto;
  height: 40px;
  line-height:40px;
  background-color: #1B764A;
  color: #fff;
  border-radius: 5px;
  position: fixed;
  bottom: 30px;
  left: 20%;
}
</style>
